function importf(obj, key_file, direId, status) {
    //导入
    if (!obj.files) {
        return false;
    }
    var f = obj.files[0];

    var ext = f.name.split(".")[1];
    if (["xls", "xlsx"].indexOf(ext) == -1) {
        layer.msg("请上传excel文档！");
        obj.outerHTML = obj.outerHTML;
        return false;
    }
    var reader = new FileReader();

    reader.onload = function (e) {
        var data = e.target.result;
        if (rABS) {
            wb = XLSX.read(btoa(fixdata(data)), {
                //手动转化
                type: "base64",
            });
        } else {
            wb = XLSX.read(data, {
                type: "binary",
            });
        }
        var seltext = wb.SheetNames[0]; //是获取Sheets中第一个Sheet的名字
        // console.log(seltext);
        // var objData = XLSX.utils.sheet_to_json(wb.Sheets[seltext]);
        var worksheet = wb.Sheets[seltext];
        var str = "";
        for (var key in worksheet) {
            if (key == "A2") {
                break;
            }
            str += (key === "!ref" ? worksheet[key] : worksheet[key].v) + ",";
        }
        var keyAry = str.split(",");
        keyAry.pop();
        keyAry.shift();
        // console.log(keyAry);
        let tempData = validationData(keyAry, key_file);
        // console.log(tempData);
        let matching = [...tempData.matching];
        let Unmatched = [...tempData.Unmatched];
        let loseTableFiled = [...tempData.loseTableFiled];

        layui.use(["jquery", "layer", "table"], function () {
            var $ = layui.$,
                transfer = layui.transfer,
                layer = layui.layer,
                util = layui.util,
                table = layui.table;
            let currentIndex = layer.open({
                type: 1,
                btn: ["提交", "取消"],
                area: ["50%", "60%"],
                content: `
            <table class="upload_pop">
              <tr class="matching">
                <td width="30%">已匹配:</td>
                <td>${matching.join(" | ")}</td>
              </tr>
              <tr class="Unmatched">
                <td width="30%">未匹配:</td>
                <td>${Unmatched.join(" | ")}</td>
              </tr>
              <tr class="loseTableFiled">
                <td width="30%">缺少表头:</td>
                <td>${loseTableFiled.join(" | ")}</td>
              </tr>
            </table>
            `,


                yes: function (index, layero) {

                    if (loseTableFiled.length || Unmatched.length) {
                        layer.msg("请调整表格！");
                        return false;
                    }


                    let formData = new FormData();
                    let file = obj.files[0];
                    formData.append("file", file);
                    formData.append("direId", direId);
                    formData.append("status", status);

                    obj.outerHTML = obj.outerHTML;



                    $.ajax({
                        type: "POST",
                        url: "/file/import",
                        data: formData,
                        processData: false,
                        contentType: false,
                        async: false,

                        success: function (rep) {


                            if (rep.status == 200) {

                                layer.msg("提交成功");

                                layer.open({
                                    title: '上传成功'
                                    , content: '上传成功！点击关闭'
                                });

                                layer.close(currentIndex);

                                // 0代表上面的表格
                                if (status == 0) {
                                    table.reload(
                                        "top_table",
                                        {
                                            page: {
                                                curr: 1, //重新从第 1 页开始
                                            },
                                            where: {},
                                        },
                                        "data"
                                    );
                                } else {
                                    table.reload(
                                        "bottom_table",
                                        {
                                            url:
                                                "/file/dire/fileNumber/" + current_fileNumber
                                                    ? current_fileNumber
                                                    : "",
                                        },
                                        "data"
                                    );
                                }

                                // console.log($(obj));

                                return false;
                            } else if (rep.status == 500) {
                                layer.msg("提交失败!" + rep.message);
                                return false;
                            }


                            /*            if (rep.status == 200) {
                                          layer.msg("提交成功");
                                          setTimeout(function () {
                                            layer.close(currentIndex);

                                            // 0代表上面的表格
                                            if (status == 0) {
                                              table.reload(
                                                "top_table",
                                                {
                                                  page: {
                                                    curr: 1, //重新从第 1 页开始
                                                  },
                                                  where: {},
                                                },
                                                "data"
                                              );
                                            } else {
                                              table.reload(
                                                "bottom_table",
                                                {
                                                  url:
                                                    "/file/dire/fileNumber/" + current_fileNumber
                                                      ? current_fileNumber
                                                      : "",
                                                },
                                                "data"
                                              );
                                            }
                                          }, 1000);
                                          // console.log($(obj));
                                          obj.outerHTML = obj.outerHTML;
                                          return false;
                                        } else if (rep.status == 500) {
                                          layer.msg("提交失败!" + rep.message);
                                          return false;
                                        }*/
                        },
                        error: function (error) {
                            layer.msg(error);
                            obj.outerHTML = obj.outerHTML;
                        },
                    });

                    return false;
                },
            });
        });
    };
    if (rABS) {
        reader.readAsArrayBuffer(f);
    } else {
        reader.readAsBinaryString(f);
    }

    function fixdata(data) {
        //文件流转BinaryString
        var o = "",
            l = 0,
            w = 10240;
        for (; l < data.byteLength / w; ++l)
            o += String.fromCharCode.apply(
                null,
                new Uint8Array(data.slice(l * w, l * w + w))
            );
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    }
}

function validationData(keyAry, key_file) {
    let Unmatched = [];
    let matching = [];
    for (let index = 0; index < keyAry.length; index++) {
        const title = keyAry[index];

        if (key_file.indexOf(title) > -1) {
            matching.push(title);
        } else {
            Unmatched.push(title);
        }
    }
    let loseTableFiled = [];
    for (let index = 0; index < key_file.length; index++) {
        const element = key_file[index];
        if (matching.indexOf(element) == -1) {
            loseTableFiled.push(element);
        }
    }
    return {
        Unmatched: Unmatched,
        matching: matching,
        loseTableFiled: loseTableFiled,
    };
}
